package com.mxx.common.conv;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.expression.BeanResolver;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.ReflectivePropertyAccessor;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Arrays;

/**
 * @author: mxx
 * @date: 2021/11/9
 * @time: 14:34
 * @Description: 描述信息
 */
public class ElUtil {

    public void parse(String express, Object data, BeanFactory beanFactory) {

        ExpressionParser parser = new SpelExpressionParser();
        TemplateParserContext templateParserContext = new TemplateParserContext("${", "}");
        SpelExpression expression = (SpelExpression) parser.parseExpression(express, templateParserContext);
        //设置动态参数
        StandardEvaluationContext context = new StandardEvaluationContext();
        BeanResolver resolver = new BeanFactoryResolver(beanFactory);
        context.setBeanResolver(resolver);
        ReflectivePropertyAccessor propertyAccessor = new ReflectivePropertyAccessor();
        context.setPropertyAccessors(Arrays.asList(propertyAccessor));

    }
}
